package sesi.ejava.stream;

import sesi.ejava.bcam.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.Toast;

public class ServerReceiverActivity extends Activity {
	public static final int REQUEST_ENABLE_BLUETOOTH = 1;

	private EditText editPositionLights;
	private EditText editDippedHeadlights;
	private EditText editMainBeam;
	private EditText editRearFogLamp;
	private EditText editFrontFogLamp;
	private EditText editWarning;
	private EditText editTemperature;
	private EditText editFanSpeed;
	private EditText editFanZone;
	private EditText editRearDefrost;
	private EditText editAirEntrance;
	private EditText editCarLock;
	private EditText editFrontRightWindow;
	private EditText editFrontLeftWindow;
	private EditText editRearLeftWindow;
	private EditText editRearRightWindow;
	private EditText editDashboardLight;
	private EditText editCockpitLight;

	private BluetoothAdapter bluetoothAdapter;
	private ServerService serverService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_receiver_activity);

		// Get local Bluetooth adapter
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		// If the adapter is null, then Bluetooth is not supported
		if (bluetoothAdapter == null) {
			Toast.makeText(this, "Bluetooth is not available",
					Toast.LENGTH_LONG).show();
			finish();
			return;
		}

		editAirEntrance = (EditText) findViewById(R.id.editAirEntrance);
		editCarLock = (EditText) findViewById(R.id.editCarLock);
		editDashboardLight = (EditText) findViewById(R.id.editDashboardLight);
		editDippedHeadlights = (EditText) findViewById(R.id.editDippedHeadlights);
		editFanSpeed = (EditText) findViewById(R.id.editFanSpeed);
		editFanZone = (EditText) findViewById(R.id.editFanZone);
		editFrontFogLamp = (EditText) findViewById(R.id.editFrontFogLamp);
		editFrontLeftWindow = (EditText) findViewById(R.id.editFrontLeftWindow);
		editFrontRightWindow = (EditText) findViewById(R.id.editFrontRightWindow);
		editMainBeam = (EditText) findViewById(R.id.editMainBeam);
		editPositionLights = (EditText) findViewById(R.id.editPositionLights);
		editRearDefrost = (EditText) findViewById(R.id.editRearDefrost);
		editRearFogLamp = (EditText) findViewById(R.id.editRearFogLamp);
		editRearLeftWindow = (EditText) findViewById(R.id.editRearLeftWindow);
		editRearRightWindow = (EditText) findViewById(R.id.editRearRightWindow);
		editTemperature = (EditText) findViewById(R.id.editTemperature);
		editWarning = (EditText) findViewById(R.id.editWarning);
		editCockpitLight = (EditText) findViewById(R.id.editCockpitLight);
	}

	@Override
	protected void onStart() {
		super.onStart();

		// If BT is not on, request that it be enabled.
		// setupChat() will then be called during onActivityResult
		if (!bluetoothAdapter.isEnabled()) {
			Intent enableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
			// Otherwise, setup the chat session
		} else {
			if (serverService == null) {
				setupServer();
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
			// When the request to enable Bluetooth returns
			if (resultCode == Activity.RESULT_OK) {
				// Bluetooth is now enabled, so set up a chat session
				setupServer();
			} else {
				// User did not enable Bluetooth or an error occured
				Toast.makeText(this, R.string.txtBluetoothNoAvailable,
						Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}

	private void ensureDiscoverable() {
		if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
			Intent discoverableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			discoverableIntent.putExtra(
					BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
			startActivity(discoverableIntent);
		}
	}

	private void setupServer() {
		serverService = ServerService.getInstance();
		serverService.setHandler(handler);

		if (serverService.getState() == ServerService.STATE_NONE) {
			ensureDiscoverable();
			serverService.start();
		}
	}

	// The Handler that gets information back from the BluetoothChatService
	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ServerService.DATA_READ:
				byte[] buffer = (byte[]) msg.obj;
				int i = 0;

				int positionLight = (buffer[0] >> i++) & 0x1;
				editPositionLights.setText(Integer.toString(positionLight));

				int dippedHeadlights = (buffer[0] >> i++) & 0x1;
				editDippedHeadlights
						.setText(Integer.toString(dippedHeadlights));

				int mainBeam = (buffer[0] >> i++) & 0x1;
				editMainBeam.setText(Integer.toString(mainBeam));

				int rearFogLamp = (buffer[0] >> i++) & 0x1;
				editRearFogLamp.setText(Integer.toString(rearFogLamp));

				int frontFogLamp = (buffer[0] >> i++) & 0x1;
				editFrontFogLamp.setText(Integer.toString(frontFogLamp));

				int warning = (buffer[0] >> i++) & 0x1;
				editWarning.setText(Integer.toString(warning));

				int rearDefrost = (buffer[0] >> i++) & 0x1;
				editRearDefrost.setText(Integer.toString(rearDefrost));

				int airEntrance = (buffer[0] >> i++) & 0x1;
				editAirEntrance.setText(Integer.toString(airEntrance));

				i = 0;
				int carLock = (buffer[1] >> i++) & 0x1;
				editCarLock.setText(Integer.toString(carLock));

				int frontRightWindow = (buffer[1] >> i++) & 0x1;
				editFrontRightWindow
						.setText(Integer.toString(frontRightWindow));

				int frontLeftWindow = (buffer[1] >> i++) & 0x1;
				editFrontLeftWindow.setText(Integer.toString(frontLeftWindow));

				int rearLeftWindow = (buffer[1] >> i++) & 0x1;
				editRearLeftWindow.setText(Integer.toString(rearLeftWindow));

				int rearRightWindow = (buffer[1] >> i++) & 0x1;
				editRearRightWindow.setText(Integer.toString(rearRightWindow));

				int dashboardLight = (buffer[1] >> i++) & 0x1;
				editDashboardLight.setText(Integer.toString(dashboardLight));

				int cockpitLight = (buffer[1] >> i++) & 0x1;
				editCockpitLight.setText(Integer.toString(cockpitLight));

				long temperature = buffer[2] + (buffer[3] << 8);
				editTemperature.setText(Long.toString(temperature));

				int fanSpeed = buffer[4];
				editFanSpeed.setText(Integer.toString(fanSpeed));

				int fanZone = buffer[5];
				editFanZone.setText(Integer.toString(fanZone));

				break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
